home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13646 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  71 lines

  1. Path: soap.news.pipex.net!pipex!usenet
  2. From: tone@dial.pipex.com (Tone)
  3. Newsgroups: comp.lang.c
  4. Subject: String assignments, please help (beginner)
  5. Date: Mon, 08 Apr 1996 15:39:36 GMT
  6. Organization: UnipalmPIPEX server (post doesn't reflect views of UnipalmPIPEX)
  7. Message-ID: <4ke05g$27q@soap.news.pipex.net>
  8. NNTP-Posting-Host: an040.du.pipex.com
  9. X-Newsreader: Forte Free Agent v0.55
  10.  
  11. I've just been learning C for the last few weeks and have come across
  12. a problem with assigning strings, which I will detail below.
  13. (Sorry the posting is so long, I'm just trying to make sure you know
  14. exactly what my problem is):
  15.  
  16. I was originally informed that the following was valid:
  17.  
  18.     char string1[30];
  19.     string1 = "Test string";
  20.  
  21. This gave me an error on the second line although when declaring I am
  22. able to use:
  23.  
  24.     char string1[30] = "Test string";
  25.  
  26. I have then read up and found the following info elsewhere (quote):
  27.  
  28.     a = "This is a string.";
  29.     is only possible if a is a char pointer.
  30.  
  31. so I arrived at the following:
  32.  
  33.     char *string1[30];
  34.     *string1 = "Test string";
  35.  
  36. This compiles okay and gives the desired result in the small program I
  37. was writing.
  38.  
  39. However, I have since found the following information (quote):
  40.  
  41.     int *c;
  42.     *c = 4;
  43.  
  44.     can and probably will give some unexpected and unwelcome results!
  45.     As the value of c is random, the memory-address to which 4 is
  46.     assigned is random, too. This means that you could change basically
  47.     any location in the memory, including machine code.
  48.  
  49. I am assuming that these "unexpected results" mentioned above will
  50. also apply to the code I have written.
  51.  
  52. The only other way I know of assigning a string is to create a loop
  53. (e.g. a "for" loop) and assign each character of the string
  54. individually and manually add the '\0' at the end. This seems
  55. extremely laborious and I'm sure it can't be the best way. (Also it
  56. may give similar "unexpected results"?)
  57.  
  58. My questions are as follows:
  59.  
  60. 1)   When using pointers as shown above, do I need to set the memory
  61. location of the string variable in some way before using it (and if
  62. so, how).
  63. 2)   Am I going about string assignments the right way. If not, could
  64. somebody please show me the most simple way.
  65.  
  66. Any help in the above would be greatly appreciated
  67. Tone
  68.  
  69. tone@dial.pipex.com
  70.  
  71.